package FreebaseMatchWiki;

import java.io.*;

import raphaelMatcher.*;

import java.util.*;
import java.util.Map.Entry;

class RphidProp{
	int phid;
	ArrayList<String>prop = new ArrayList<String>();
}
public class StatisticMatchResult_ByType {

	/**
	 * @param args
	 */
	
	//get following result
	//<rphid> <Num_property_it_can_match> <article_title> <matched_properies_delimited_by_|;before @ is type, after @ is property>




	//get following result
	//<prop@type> <Num_Matched Articles> <Article_List>
	

	
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		String dir = args[0];
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(args[1]),"utf-8");
		//DelimitedWriter dw = new DelimitedWriter(args[1]);
//		String rphid_wpid_file = args[2];
	
//		HashMap<Integer,String>rphid_title = new HashMap<Integer,String>();
//		BufferedReader br0 = new BufferedReader(new InputStreamReader
//				(new FileInputStream(rphid_wpid_file), "utf-8"));	
//		String line0= null;
//		int ln = 0;
//		while((line0 = br0.readLine())!=null){
//			ln++;
//			if(ln % 100000==0)System.out.print(".");
//			String []split = line0.split("\t");
//			rphid_title.put(Integer.parseInt(split[0]), split[2]);
//		}
		HashMap<String, ArrayList<Integer>>map = new HashMap<String, ArrayList<Integer>>();
		System.out.println("Load rphid_title finish");
		File f = new File(dir);
		String []list = f.list();
		//HashMap<Integer, RphidProp>map = new HashMap<Integer, RphidProp>();
		for(int i=0;i<list.length;i++){
			//DelimitedReader dr = new DelimitedReader(dir+"/"+list[i]);
			BufferedReader br = new BufferedReader(new InputStreamReader
					(new FileInputStream(dir+"/"+list[i]), "utf-8"));	
			String []abc = null;
			String line = "";
			ArrayList<String[]>block = new ArrayList<String []>();
			int wn = 0;
			while((line = br.readLine())!=null){
				abc= line.split("\t");
				//int rphid = Integer.parseInt(abc[0]);
				if(block.size() == 0 || block.get(0)[0].equals(abc[0])){
					block.add(abc);
				}else{
					int rphid = Integer.parseInt( block.get(0)[0]);
					Iterator<String []>it = block.iterator();
					while(it.hasNext()){
						String []split = it.next();
						String typeprop = split[3]+"@"+split[4];
						ArrayList<Integer> pagelist = map.get(typeprop);
						if(pagelist == null){
							pagelist = new ArrayList<Integer>();
							pagelist.add(rphid);
							map.put(typeprop, pagelist);
						}else{
							pagelist.add(rphid);
						}
						//System.out.println(rphid+""+" "+howManyPropMatched+" "+rphid_title.get(rphid)+" "+typePropList);
					}
					
//					osw.write(rphid+"\t"+howManyPropMatched+"\t"+typePropList+"\n");
					wn++;
					if(wn % 100000==0)System.out.print(".");
					block.clear();
					block.add(abc);
				}
			}
			br.close();
		}
		Iterator<Entry<String,ArrayList<Integer>>>it = map.entrySet().iterator();
		while(it.hasNext()){
			Entry<String,ArrayList<Integer>> e = it.next();
			String proptype = e.getKey();
			ArrayList<Integer>matchedIntegers =e.getValue();
			osw.write(proptype+"\t"+matchedIntegers.size()+"\t"+matchedIntegers+"\n");
		}
		osw.close();
		//drRphidTitle.close();
	
	}
}
